package web.base.user.provider;

import org.springframework.beans.factory.annotation.Autowired;
import web.base.user.convertor.UserConvertor;
import web.base.user.exception.UserException;
import web.base.user.facade.UserFacade;
import web.base.user.message.UserRequest;
import web.base.user.message.UserResponse;
import web.base.user.message.UserSearchRequest;
import web.base.user.message.UserSearchResponse;
import web.base.user.service.UserService;
import web.common.core.annotation.Provider;

/**
 * @author Jin Zheng
 * @since 2022-01-03
 */
@Provider("baseUserProvider")
public class UserProvider implements UserFacade {
	private UserService userService;

	public UserProvider() {
	}

	@Override
	public UserResponse register(UserRequest request) throws UserException {
		var entity = UserConvertor.toEntity(request);
		var resp = userService.register(entity);
		return UserConvertor.toResponse(resp);
	}

	@Override
	public UserResponse login(UserRequest request) throws UserException {
		var entity = UserConvertor.toEntity(request);
		var resp = userService.login(entity);
		return UserConvertor.toResponse(resp);
	}

	@Override
	public UserResponse get(Integer id) throws UserException {
		var entity = userService.get(id);
		return UserConvertor.toResponse(entity);
	}

	@Override
	public UserSearchResponse search(UserSearchRequest request) throws UserException {
		var entity = UserConvertor.toEntity(request);
		var page = UserConvertor.toPage(request);
		var resp = userService.list(entity, page);
		return UserConvertor.toPage(resp);
	}

	@Autowired
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
}
